/*
 * Copyright © 2018 CODESTD.COM Inc. All rights reserved.
 */
package com.codestd.security.shiro.filter;

import com.codestd.security.base.ResponseBean;
import com.codestd.security.shiro.ResponseUtils;
import com.codestd.security.shiro.StatelessWebSubjectFactory;
import com.codestd.security.shiro.jwt.JwtTokenManager;
import org.apache.shiro.util.StringUtils;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * 注销
 *
 * @author jaune
 * @since 1.0.0
 */
public class StatelessLogoutFilter extends LogoutFilter {

    private JwtTokenManager jwtTokenManager;

    public void setJwtTokenManager(JwtTokenManager jwtTokenManager) {
        this.jwtTokenManager = jwtTokenManager;
    }

    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) {
        String token = WebUtils.toHttp(request).getHeader(StatelessWebSubjectFactory.AUTH_HEADER_NAME);
        if (StringUtils.hasText(token)) {
            this.jwtTokenManager.deleteToken(token);
        }
        ResponseUtils.printResponseBean(response, ResponseBean.builder()
                .success(true).status(200).message("注销成功").build());
        return false;
    }
}
